<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_id = this.$get('model_id');

    if(in_right_admin('ModelFieldWhere/add')) btns['添加'] = function(){ app.iframe('edit?'+app.param()) }
    if(in_right_admin('ModelFieldWhere/delete')) btns['彻底删除'] = function(){
      app.$refs['list_url'].delete('', function (){
        app.iframe_exe('/model/all?id='+model_id, 'model_field_where_list');
      });
    }
    let table_map = this.admin_config('status_alias_map.model_field_where.table');
    let method_map = {
      'eq':'等于', 'neq':'不等于',
      'gt':'大于', 'egt':'大于或等于',
      'lt':'小于', 'elt':'小于或等于',
      'like':'包含', 'notlike':'不包含',
      'in':'在列表中', 'notin':'不在列表中',
      'between':'介于之间', 'notbetween':'不介于之间',
    };
    let type_map = this.field_type(this.field_type.CAN_MODEL_FIELD_WHERE, 'label');
    let forms_config = {};
    if (!model_id) {
      forms_config.model_id = {label:'模型',name:'where[model_id][eq]',type:'select_one',config:[],value:''}
    }
    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      model_alias:{name:'model_alias', label:'模型', width:'100px'},
      name:{name:'name', label:'字段名',width:'130px',align:'left'},
      method:{name:'method', label:'检索方式', type:'select_one', config:method_map, width:'80px'},
      label:{name:'label', label:'别名', width:'100px'},
      table:{name:'table', label:'所属表', type:'select_one', config:table_map, width:'60px'},
      tips:{name:'tips', label:'简介', min_width:'80px'},
      type:{name:'type', label:'字段类型',type:'select_one',config:type_map,width:'100px'},
      sort:{name:'sort', label:'排序', type:'text_one', width:'80px', can_edit:true, onchange:this.edit},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/model_field_where/'+(is_del?'recycle':'list')+(model_id?'?where[model_id][eq]='+model_id:''),
        title: "模型检索字段列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_id = this.model_id;
      return s94Mjs.param_encode(data);
    },
    to_edit(row){
      let can_edit = in_right_admin('ModelFieldWhere/edit')
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
    edit(row,col,v){
      let app = this;
      if(!in_right_admin('ModelFieldWhere/edit')) return;
      ajax(`/admin/model_field_where/edit?id=${row.id}&${col.name}=${v}`, function (res){
        if (col.type=='radio_switch' && res.data==0) app.$refs['list_url'].list();
        if (col.type!='radio_switch' && res.data>0) app.$refs['list_url'].list();
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    if (this.config.forms_config.model_id){
      ajax({url:'/admin/model/list',cache:true},(res)=>{
        this.config.forms_config.model_id.config = this.decode_config(res.data.list, {content_key:'alias', value_key:'id'});
        return false
      })
    }
  }
}
</script>
